package fr.neyb.bernouilli.problem011;

/**
 * returns 0 at each endof buff used
 * 
 * @author adeconin
 * 
 */
public abstract class IntReader {
	protected int cursorLine = 0;
	protected int cursorPos = 0;

	protected final int[][] content;

	private int[] buff;
	private int buffPos = 0;

	public IntReader(int[][] content, int suiteLength) {
		buff = new int[suiteLength];
		this.content = content;
	}

	public int readNext() {
		if (cursorLine == -1 && cursorPos == -1)
			return -1;

		int res = add(content[cursorLine][cursorPos]);
		if (incrCursor())
			add(0);
		return res;
	}

	private int add(int i) {
		buff[buffPos] = i;
		buffPos = (buffPos + 1) % buff.length;
		return prod();
	}

	private int prod() {
		int res = 1;
		for (int i : buff)
			res *= i;
		return res;
	}

	/**
	 * cursorLine == -1 && cursorPos == -1 is 
	 * @return true if switch line/row/column... or end position
	 */
	protected abstract boolean incrCursor();
}
